package com.xcw.test;

import com.xcw.util.SmallTool;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @Author bhx
 * @Date 2022/7/19 1:49
 * @Description TODO
 */
public class MainTestCompletableFuture_3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        SmallTool.printTimeAndThread("小白进入餐厅");
        SmallTool.printTimeAndThread("小白点餐：牛肉饭");
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(()->{
            SmallTool.printTimeAndThread("厨师炒菜");
            SmallTool.sleepMills(1000L);
            SmallTool.printTimeAndThread("厨师上盘");
            return "饭做好了！！";
        }).thenCombine(CompletableFuture.supplyAsync(()->{
            SmallTool.sleepMills(2000L);
            SmallTool.printTimeAndThread("服务员端盘！");
            SmallTool.printTimeAndThread("服务员完成服务！");
            return "服务员服务完成";
        }),(dish,rich)->{
            SmallTool.printTimeAndThread("厨师上盘");
            SmallTool.printTimeAndThread("服务员完成服务！");
            return "同时完成";
        });
        System.out.println("------" + completableFuture.getNow("服务员服务完成"));
        System.out.println("----------------------------------" + completableFuture.get());
        SmallTool.printTimeAndThread("小白付钱");

        SmallTool.printTimeAndThread(String.format("小白离开餐厅",completableFuture.join()));
    }
}
